using Gtk;

namespace Zoo
{

public class App
{
    private string ui_file;

    private Window main_window;
    private VBox main_vbox;
    private Frame x_frame;

    private Terminal term;
    private XWidget xwidget;

    public App()
    {
        this.ui_file = "zoo.ui";
        init_app();
    }

    ~App()
    {
        stderr.printf("destructor 1\n");
        this.xwidget.stop_xserver();
        stderr.printf("destructor 2\n");
    }

    private void init_app()
    {
        try {
            var builder = new Builder();
            builder.add_from_file(this.ui_file);
            builder.connect_signals(null);
            main_window = builder.get_object("window") as Window;
            main_vbox = builder.get_object("main_vbox") as VBox;
            x_frame = builder.get_object("x_frame") as Frame;
        } catch (Error e) {
            stderr.printf("Could not load UI: %s\n", e.message);
            Posix.exit(1);
        }

        // Create embedded X Server widget
        xwidget = new XWidget();
        x_frame.add(xwidget);

        // Start the embedded X Server
        GLib.Idle.add(xwidget.start_xserver);

        // Create terminal widget
        term = new Terminal();
        term.child_exited += on_terminal_child_exit;
        main_vbox.pack_end(term, true, true, 0);
        term.grab_focus();
        term.start();
    }

    public void run()
    {
        main_window.show_all();
        Gtk.main();
    }

    public static void quit()
    {
        stderr.printf("quit\n");
        Gtk.main_quit();
    }

    [CCode (instance_pos = -1)]
    public void start_cb(Widget source)
    {
        stderr.printf("start\n");
    }

    [CCode (instance_pos = -1)]
    public static void stop_cb(Widget source)
    {
        stderr.printf("stop\n");
    }

    [CCode (instance_pos = -1)]
    public static void restart_cb(Widget source)
    {
        stderr.printf("restart\n");
    }

    [CCode (instance_pos = -1)]
    public static void kill_cb(Widget source)
    {
        stderr.printf("kill\n");
    }

    [CCode (instance_pos = -1)]
    public static void prefs_cb(Widget source)
    {
        stderr.printf("preferences\n");
    }

    [CCode (instance_pos = -1)]
    public static void about_cb(Widget source)
    {
        stderr.printf("about\n");
    }

    [CCode (instance_pos = -1)]
    public static void on_window_delete_event(Widget widget, Gdk.EventKey e)
    {
        stderr.printf("on_window_delete_event\n");
        quit();
    }

    [CCode (instance_pos = -1)]
    public static void quit_cb(Button source)
    {
        stderr.printf("quit_cb\n");
        quit();
    }

    [CCode (instance_pos = -1)]
    public static void on_target_combo_changed(ComboBox combo)
    {
        stderr.printf("on_target_combo_changed\n");
    }

    [CCode (instance_pos = -1)]
    public static void on_transp_combo_changed(ComboBox combo)
    {
        stderr.printf("on_transp_combo_changed\n");
    }

    [CCode (instance_pos = -1)]
    public void on_terminal_child_exit(Zoo.Terminal terminal)
    {
        stderr.printf("on_terminal_child_exit\n");
        quit();
    }
}

} // namespace Zoo

/*
    public void start()
    {
        send_command("af-sb-init.sh start");
    }

    public void stop()
    {
        send_command("af-sb-init.sh stop");
    }

    public void restart()
    {
        send_command("af-sb-init.sh restart");
    }

    public void killall(VteTerminal *terminal)
    {
        send_command("sb-conf killall -s 9");
    }
*/

/*

public static void app_quit()
{
    //xwidget_free(app.xwidget);
    Gtk.main_quit();
}
*/

static int main(string[] args)
{
    Gtk.init(ref args);
    Zoo.App app = new Zoo.App();
    app.run();
    return 0;
}

